package com.chinatechstar.admin.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.chinatechstar.account.entity.SysUserDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.admin.entity.SysUserGroup;
import com.chinatechstar.admin.service.SysUserGroupService;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 用户组 的控制层
 */
@Api(value = "用户组 的控制层", tags = {"用户组"})
@RestController
@RequestMapping("/sysusergroup")
public class SysUserGroupController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysUserGroupService sysUserGroupService;

    /**
     * 查询 用户组 所有数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 用户组 所有数据", tags = {"用户组"})
    @GetMapping(path = "/findSysUserGroupAll")
    public ListResult<Object> findSysUserGroupAll() {
        List<SysUserGroup> list = sysUserGroupService.findSysUserGroupAll();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询 用户组 分页
     *
     * @param currentPage 当前页数
     * @param pageSize    每页记录数
     * @param name        名称
     * @param sorter      排序
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 用户组 分页", tags = {"用户组"})
    @GetMapping(path = "/findSysUserGroupByCondition")
    public ListResult<Object> findSysUserGroupByCondition(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                          @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                          @RequestParam(name = "name", required = false) String name,
                                                          @RequestParam(name = "sorter", required = false) String sorter) {
        Map<String, Object> data = sysUserGroupService.findSysUserGroupByCondition(currentPage, pageSize, name, sorter);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 根据查询条件导出 用户组
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出 用户组", tags = {"用户组"})
    @PostMapping(path = "/exportSysUserGroup")
    public void exportSysUserGroup(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "名称", "创建时间");
            List<LinkedHashMap<String, Object>> dataList = sysUserGroupService.findSysUserGroupForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "管理", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据主键ID查询 用户组 数据
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数据查询 用户组 数据", tags = {"用户组"})
    @GetMapping(path = "/findSysUserGroupById")
    public ActionResult findSysUserGroupById(@RequestParam(name = "id") Long id) {
        SysUserGroup data = sysUserGroupService.findSysUserGroupById(id);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 根据主键ID数组查询 用户组 数据
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组查询 用户组 数据", tags = {"用户组"})
    @GetMapping(path = "/findSysUserGroupByIds")
    public ListResult<Object> findSysUserGroupByIds(@RequestParam(name = "ids") Long[] ids) {
        List<SysUserGroup> list = sysUserGroupService.findSysUserGroupByIds(ids);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询用户组下的所有用户数据
     *
     * @param id          ID
     * @param currentPage 当前页数
     * @param pageSize    每页记录数
     * @return ActionResult
     */
    @ApiOperation(value = "查询用户组下的所有用户数据", tags = {"用户组"})
    @GetMapping(path = "/findUserByUserGroupId")
    public ListResult<Object> findUserByUserGroupId(@RequestParam(name = "id") Long id,
                                                    @RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                    @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Map<String, Object> list = sysUserGroupService.findUserByUserGroupId(currentPage, pageSize, id);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 新增 用户组
     *
     * @param sysUserGroup 用户组 实体
     * @return ActionResult
     */
    @ApiOperation(value = "新增 用户组", tags = {"用户组"})
    @PostMapping(path = "/addSysUserGroup")
    public ActionResult addSysUserGroup(@Validated(InsertValidator.class) @RequestBody SysUserGroup sysUserGroup) {
        sysUserGroupService.insertSysUserGroup(sysUserGroup);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量新增 用户组
     *
     * @param sysUserGroups 用户组 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量新增 用户组", tags = {"用户组"})
    @PostMapping(path = "/addSysUserGroups")
    public ActionResult addSysUserGroups(@Validated(InsertValidator.class) @RequestBody List<SysUserGroup> sysUserGroups) {
        sysUserGroupService.insertSysUserGroups(sysUserGroups);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量将用户加入用户组
     *
     * @param userGroupId 用户组id
     * @param userId      用户数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量将用户加入用户组", tags = {"用户组"})
    @PostMapping(path = "/insertUserToUserGroup")
    public ActionResult insertUserToUserGroup(@RequestParam(name = "userGroupId") Long userGroupId,
                                              @RequestParam(name = "userId") Long[] userId) {
        sysUserGroupService.insertUserToUserGroup(userGroupId, userId);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑 用户组
     *
     * @param sysUserGroup 用户组 实体
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 用户组", tags = {"用户组"})
    @PutMapping(path = "/updateSysUserGroup")
    public ActionResult updateSysUserGroup(@Validated(UpdateValidator.class) @RequestBody SysUserGroup sysUserGroup) {
        sysUserGroupService.updateSysUserGroup(sysUserGroup);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量编辑 用户组
     *
     * @param sysUserGroups 用户组 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量编辑 用户组", tags = {"用户组"})
    @PostMapping(path = "/updateSysUserGroups")
    public ActionResult updateSysUserGroups(@Validated(UpdateValidator.class) @RequestBody List<SysUserGroup> sysUserGroups) {
        sysUserGroupService.updateSysUserGroups(sysUserGroups);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 用户组
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID删除 用户组", tags = {"用户组"})
    @PostMapping(path = "/deleteSysUserGroupById")
    public ActionResult deleteSysUserGroupById(@RequestParam(name = "id") Long id) {
        sysUserGroupService.deleteSysUserGroupById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 用户组
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量删除 用户组", tags = {"用户组"})
    @PostMapping(path = "/deleteSysUserGroupByIds")
    public ActionResult deleteSysUserGroupByIds(@RequestParam(name = "id") Long[] ids) {
        sysUserGroupService.deleteSysUserGroupByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 用户组
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID软删除 用户组", tags = {"用户组"})
    @PostMapping(path = "/softDeleteSysUserGroupById")
    public ActionResult softDeleteSysUserGroupById(@RequestParam(name = "id") Long id) {
        sysUserGroupService.softDeleteSysUserGroupById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 软删除 用户组
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量软删除 用户组", tags = {"用户组"})
    @PostMapping(path = "/softDeleteSysUserGroupByIds")
    public ActionResult softDeleteSysUserGroupByIds(@RequestParam(name = "id") Long[] ids) {
        sysUserGroupService.softDeleteSysUserGroupByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

}
